﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LS.Models;
using LS.ModelLogicHelpers;

namespace LS.Controllers
{
    public class LFHController : Controller
    {
        private LostFoundHistoryContext db = new LostFoundHistoryContext();
        LostFoundHistoryLogicHelper logic = new LostFoundHistoryLogicHelper();
        //
        // GET: /LFH/

        public ActionResult Index()
        {
            var lostfoundhistories = db.LostFoundHistories.Include(l => l.RelatedMemberCard);
            return View(lostfoundhistories.ToList());
        }

        //
        // GET: /LFH/Details/5

        public ActionResult Details(int id = 0)
        {
            LostFoundHistory lostfoundhistory = db.LostFoundHistories.Find(id);
            if (lostfoundhistory == null)
            {
                return HttpNotFound();
            }
            return View(lostfoundhistory);
        }

        //
        // GET: /LFH/Create

        public ActionResult Create()
        {
            ViewBag.MemberCardId = new SelectList(db.MemberCards, "Id", "Description");
            return View();
        }

        public ActionResult IFindIt() 
        {
            return View();
        }

        [HttpGet]
        public ActionResult IFindIt(string SeiralNumber) 
        {
            FindModel fm = new FindModel() { SeiralNumber = SeiralNumber };

            return View(fm);
        }



        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult IFindIt(FindModel model) 
        {
            if (logic.Found(model.SeiralNumber, model.ReportContact)) 
            {
                return RedirectToAction("Thanks", "Home");
            }
            return View();
        }

        public ActionResult Found(int MemberCardId) 
        {
            var model = logic.GetResult(MemberCardId);
            return View(model);
        }
        //
        // POST: /LFH/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(LostFoundHistory lostfoundhistory)
        {
            if (ModelState.IsValid)
            {
                db.LostFoundHistories.Add(lostfoundhistory);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.MemberCardId = new SelectList(db.MemberCards, "Id", "Description", lostfoundhistory.MemberCardId);
            return View(lostfoundhistory);
        }

        //
        // GET: /LFH/Edit/5

        public ActionResult Edit(int id = 0)
        {
            LostFoundHistory lostfoundhistory = db.LostFoundHistories.Find(id);
            if (lostfoundhistory == null)
            {
                return HttpNotFound();
            }
            ViewBag.MemberCardId = new SelectList(db.MemberCards, "Id", "Description", lostfoundhistory.MemberCardId);
            return View(lostfoundhistory);
        }

        //
        // POST: /LFH/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(LostFoundHistory lostfoundhistory)
        {
            if (ModelState.IsValid)
            {
                db.Entry(lostfoundhistory).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.MemberCardId = new SelectList(db.MemberCards, "Id", "Description", lostfoundhistory.MemberCardId);
            return View(lostfoundhistory);
        }

        //
        // GET: /LFH/Delete/5

        public ActionResult Delete(int id = 0)
        {
            LostFoundHistory lostfoundhistory = db.LostFoundHistories.Find(id);
            if (lostfoundhistory == null)
            {
                return HttpNotFound();
            }
            return View(lostfoundhistory);
        }

        //
        // POST: /LFH/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            LostFoundHistory lostfoundhistory = db.LostFoundHistories.Find(id);
            db.LostFoundHistories.Remove(lostfoundhistory);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}